﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YY.Web.MyAgent.financial_information.AddBank
{
    public partial class AddBankStep2 : BaseFunction.LoginHelp
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string TrueName = (base.GetSessionModel() as YY.Model.agent_users).true_name.ToString();
                string ID = (base.GetSessionModel() as YY.Model.agent_users).id_number.ToString();
                if (string.IsNullOrEmpty(TrueName) || string.IsNullOrEmpty(ID))
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>AlarmInfor();</script>");
                }
                else
                {
                    txt_name.Text = TrueName;
                    txt_Id.Text = ID;
                }
            }
        }

        /// <summary>
        /// 添加银行卡事件
        /// </summary>
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (Session["SMS"] != null)
            {
                string smscode = txt_smscode.Value.Trim();
                string code = Session["SMS"].ToString().Split(',')[0];
                if (smscode == code)
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>CheckCodeHide();</script>");
                    string UId = (base.GetSessionModel() as YY.Model.agent_users).user_guid.ToString();
                    string BankId = span_bankid.Value;
                    string BankName = span_bankName.Value;
                    string OpenAccountAddress = txt_OpenAccountAddress.Text.ToString().Trim();
                    string AccountBranch = txt_AccountBranch.Text.ToString().Trim();
                    string BankCardNo = txt_BankCardNo.Text.ToString().Trim();
                    string IsDefaultCard = txt_IsDefaultCard.Checked == true ? "1" : "0";
                    string ReservePhonenumber = txt_ReservePhonenumber.Text.Trim();
                    if (IsDefaultCard == "1")
                    {
                        string str = "update tb_YiyangBankUserInfor set IsDefaultCard = 0 where UseId = '" + UId + "'";
                        string sqlstr = "insert into tb_YiyangBankUserInfor(BankId, UseId, OpenAccountAddress, AccountBranch, BankCardNo, IsDefaultCard,ReservePhonenumber, QuickPayment, BandTime) values('" + BankId + "','" + UId + "','" + OpenAccountAddress + "','" + AccountBranch + "','" + BankCardNo + "','1','" + ReservePhonenumber + "','1','" + DateTime.Now.ToString() + "')";
                        List<string> listSql = new List<string>();
                        listSql.Add(str);
                        listSql.Add(sqlstr);
                        if (ExecuteSqlTran(listSql) > 0)
                        {
                            Response.Redirect("AddBankStep3.aspx");
                        }
                        else
                        {
                            Maticsoft.Common.MessageBox.Show(this.Page, "添加银行卡失败,请重新操作!");
                        }
                    }
                    else
                    {
                        if (AddBank(UId, BankId, BankName, OpenAccountAddress, AccountBranch, BankCardNo, IsDefaultCard, ReservePhonenumber) > 0)
                        {
                            Response.Redirect("AddBankStep3.aspx");
                        }
                        else
                        {
                            Maticsoft.Common.MessageBox.Show(this.Page, "添加银行卡失败,请重新操作!");
                        }
                    }
                    
                }
                else
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>CheckCodeShow();</script>");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>CheckCodeShow();</script>");
            }
        }

        /// <summary>
        /// 会员-银行卡关系绑定
        /// </summary>
        public int AddBank(string userid, string BankId,string BankName, string OpenAccountAddress, string AccountBranch, string BankCardNo, string IsDefaultCard, string ReservePhonenumber)
        {
            YY.Model.YiyangBankUserInfor model = new Model.YiyangBankUserInfor();
            model.UseId = userid;
            model.BankId = Convert.ToInt32(BankId);
            model.BankName = BankName;
            model.OpenAccountAddress = OpenAccountAddress;
            model.AccountBranch = AccountBranch;
            model.BankCardNo = BankCardNo;
            model.IsDefaultCard = Convert.ToInt32(IsDefaultCard);
            model.ReservePhonenumber = ReservePhonenumber;
            model.QuickPayment = 1;
            model.BandTime = DateTime.Now;
            int number = BaseFunction.StaticTableClass.YiyangBankUserInforHelp.bll_YiyangBankUserInfor.Add(model);
            return number;
        }

        public int ExecuteSqlTran(List<string> list)
        {
            return BaseFunction.StaticTableClass.YiyangBankUserInforHelp.ExecuteSqlTran(list);
        }
    }
}